package com.yunhe.sms.dao.system.impl;

import com.yunhe.commens.dao.impl.BaseDaoImpl;
import com.yunhe.commens.dto.Pagedao;
import com.yunhe.commens.util.JdbcHelp;
import com.yunhe.sms.dao.system.RoleDao;
import com.yunhe.sms.entity.system.Role;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/10/24 0024 10:25
 * @Description: 实现RoleDao 接口
 */
public class RoleDaoImpl extends BaseDaoImpl<Role> implements RoleDao{

    @Override
    public Long save(Role role) {
        String sql="insert into sys_role(name,status,remark,deleted,created_time) values(?,?,?,0,now() )";
        QueryRunner queryRunner = new QueryRunner ( JdbcHelp.getDataSource () );
        Long id = null;
        try {
             id=queryRunner.insert ( sql,new ScalarHandler<Long>(), role.getName (), role.getStatus (), role.getRemark () );
        } catch (SQLException e) {
            e.printStackTrace ();
        }
        return id;
    }

    @Override
    public void saveRolePremi(Long roleId,Long premiId ) {
        String sql= " insert into sys_role_premi(role_id,premi_id) values(?,?)";
       /* QueryRunner queryRunner = new QueryRunner ( JdbcHelp.getDataSource () );
        try {
            queryRunner.batch ( sql,params);
        } catch (SQLException e) {
            e.printStackTrace ();
        }*/
        super.cut ( sql,roleId,premiId );
    }

    @Override
    public void deletedRolPremi(Long rolid) {
        String sql="delete  from sys_role_premi where role_id=?";
        super.cut ( sql,rolid );
    }

    @Override
    public void deleted(Long id) {
        String sql="update sys_role set deleted=1 where id=?";
        super.cut ( sql,id );
    }

    @Override
    public void update(Role role) {
        String sql= "update sys_role set name=?,status=?,remark=? where deleted=0 and id=?";
        super.cut ( sql,role.getName (),role.getStatus (),role.getRemark (),role.getId ());
    }

    @Override
    public Role find(Long id) {
        String sql="select id,name,status,remark from sys_role where deleted=0 and id=?";
        return  super.quaryOne ( sql,id );
    }

    @Override
    public List<Role> findList() {
        String sql=" select id,name,status,remark from sys_role where deleted=0 ";
        return super.quaryList ( sql );
    }

    @Override
    public List<Role> findListStatu() {
        String sql=" select id,name,status,remark from sys_role where deleted=0 and status=1";
        return super.quaryList ( sql );
    }

    @Override
    public Pagedao<Role> page(int offset, int limit) {
        String listsql=" select id,name,status,remark from sys_role where deleted=0 ";
        String countsql=" select count(*) from sys_role where deleted=0 ";
        return super.quaryPage ( listsql,countsql,offset,limit );
    }
}
